package net.zdsoft.wpchat.support.action.auth;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import net.zdsoft.keel.util.StringUtils;
import net.zdsoft.keel.util.UUIDUtils;
import net.zdsoft.keel.util.Validators;
import net.zdsoft.wpchat.android.client.BasicAction;
import net.zdsoft.wpchat.support.entity.AuthGroup;
import net.zdsoft.wpchat.support.service.AuthService;

/**
 * 群组维护Action
 * 
 * @author xuan
 * @version $Revision: 1.0 $, $Date: 2014-9-22 下午8:48:45 $
 */
public class GroupAction extends BasicAction {
    private static final long serialVersionUID = -3576602880324314735L;

    private List<AuthGroup> groupList = new ArrayList<AuthGroup>();

    @Resource
    private AuthService authService;

    private String id;
    private String name;
    private String description;

    private String[] groupIds;

    private String inputStream;

    /**
     * 查询群组列表
     * 
     * @return
     */
    public String listGroup() {
        groupList = authService.getAllGroup(getSupportMemoryUser().getId());
        return SUCCESS;
    }

    /**
     * 添加群组或者修改群组
     * 
     * @return
     */
    public String addModifyGroup() {
        if (Validators.isEmpty(name)) {
            inputStream = getError("请输入组名");
            return SUCCESS;
        }
        if (StringUtils.getRealLength(name) > 100) {
            inputStream = getError("组名不能超过100个字符(一个汉字为两个字符)");
            return SUCCESS;
        }
        if (StringUtils.getRealLength(description) > 255) {
            inputStream = getError("描述不能超过255个字符(一个汉字为两个字符)");
            return SUCCESS;
        }

        AuthGroup authGroup = new AuthGroup();
        authGroup.setName(name);
        authGroup.setDescription(description);
        if (Validators.isEmpty(id)) {
            authGroup.setId(UUIDUtils.newId());
            authGroup.setCreationTime(new Date());
            authGroup.setModifiedTime(new Date());
            authService.addGroup(authGroup);
            inputStream = getMessage("用户组添加成功");
        }
        else {
            authGroup.setId(id);
            authService.modifyGroup(authGroup);
            inputStream = getMessage("用户组修改成功");
        }

        return SUCCESS;
    }

    /**
     * 批量删除群组
     * 
     * @param groupIds
     * @return
     */
    public String removeGroup() {
        inputStream = getMessage("删除成功");
        authService.removeGroup(groupIds);
        return SUCCESS;
    }

    public List<AuthGroup> getGroupList() {
        return groupList;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setGroupList(List<AuthGroup> groupList) {
        this.groupList = groupList;
    }

    public String getInputStream() {
        return inputStream;
    }

    public void setInputStream(String inputStream) {
        this.inputStream = inputStream;
    }

    public String[] getGroupIds() {
        return groupIds;
    }

    public void setGroupIds(String[] groupIds) {
        this.groupIds = groupIds;
    }

}
